/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.opgea.qms.service.impl;

import com.opgea.qms.dao.TokenDAO;
import com.opgea.qms.domain.entities.Token;
import com.opgea.qms.domain.entities.TokenDetails;
import com.opgea.qms.domain.model.TokenStatusReplyModel;
import com.opgea.qms.domain.qualifiers.TokenStatusQualifier;
import com.opgea.qms.service.TokenService;
import com.opgea.qms.web.form.TokenDetailsDTO;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 *
 * @author Ramesh
 */
@Service
public class TokenServiceImpl implements TokenService{

    @Autowired
    private TokenDAO tokenDAO;
    

    @Override
    public Integer getMaxTokenNumber(Long companyId, String dateString) {
        if(tokenDAO.getMaxTokenNumber(companyId, dateString) == null){
            return 0;
        }else{
            return tokenDAO.getMaxTokenNumber(companyId, dateString);
        }
    }
    
    @Override
    public Token create(Token token) {
        return tokenDAO.create(token);
    }

    @Override
    public Token update(Token token) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Token find(Long id) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Token findAll() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<TokenDetailsDTO> findAllByCompanyId(Long companyId, Date date) {
        List<Token> tokenList = new ArrayList<Token>();
        if(tokenDAO.findAll(companyId, date) != null){
            tokenList = tokenDAO.findAll(companyId, date);
        }
        List<TokenDetailsDTO> tokenDetailsList = new ArrayList<TokenDetailsDTO>();
        if(tokenList != null){
            for(Token token: tokenList){
                //System.out.println("Token: "+token);
                TokenDetails tokenDetails = token.getTokenDetails();
                TokenDetailsDTO tdm = new TokenDetailsDTO();
                tdm.setTokenNo(tokenDetails.getId().toString());
                tdm.setFirstName(tokenDetails.getFirstName());
                tdm.setMiddleInitial(tokenDetails.getMiddleInitial());
                tdm.setLastName(tokenDetails.getLastName());
                
                if(tokenDetails.getId() != null){
                    tdm.setId(tokenDetails.getId());
                }
                if(tokenDetails.getToken().getTokenNo() != null){
                    tdm.setTokenNo(tokenDetails.getToken().getTokenNo().toString());
                }
                if(tokenDetails.getPrefix() != null){
                    tdm.setPrefix(tokenDetails.getPrefix().toString());
                }
                if(tokenDetails.getFirstName() != null){
                    tdm.setFirstName(tokenDetails.getFirstName());
                }
                if(tokenDetails.getMiddleInitial() != null){
                    tdm.setMiddleInitial(tokenDetails.getMiddleInitial());
                }
                if(tokenDetails.getLastName() != null){
                    tdm.setLastName(tokenDetails.getLastName());
                }
                if(tokenDetails.getMobileNo() != null){
                    tdm.setMobileNo(tokenDetails.getMobileNo());
                }
                if(tokenDetails.getEmailId() != null){
                    tdm.setEmailId(tokenDetails.getEmailId());
                }
                if(tokenDetails.getDestination() != null){
                    tdm.setDestinationId(tokenDetails.getDestination().getId());
                    tdm.setDestinationName(tokenDetails.getDestination().getName());
                }
                if(tokenDetails.getTokenStatus() != null){
                    tdm.setTokenStatus(tokenDetails.getTokenStatus().ordinal());
                }
                //System.out.println("TokenDetailsDTO: "+tdm);
                tokenDetailsList.add(tdm);
            }
        }
        return tokenDetailsList;
    }

    @Override
    public List<TokenDetailsDTO> findAllByCompanyIdAndStatus(Long companyId, Date date, TokenStatusQualifier qualifier) {
        List<Token> tokenList = new ArrayList<Token>();
        if(tokenDAO.findAll(companyId, date) != null){
            tokenList = tokenDAO.findAllByStatus(companyId, date, qualifier);
        }
        List<TokenDetailsDTO> tokenDetailsList = new ArrayList<TokenDetailsDTO>();
        if(tokenList != null){
            for(Token token: tokenList){
                //System.out.println("Token: "+token);
                TokenDetails tokenDetails = token.getTokenDetails();
                TokenDetailsDTO tdm = new TokenDetailsDTO();
                tdm.setTokenNo(tokenDetails.getId().toString());
                tdm.setFirstName(tokenDetails.getFirstName());
                tdm.setMiddleInitial(tokenDetails.getMiddleInitial());
                tdm.setLastName(tokenDetails.getLastName());
                
                if(tokenDetails.getId() != null){
                    tdm.setId(tokenDetails.getId());
                }
                if(tokenDetails.getToken().getTokenNo() != null){
                    tdm.setTokenNo(tokenDetails.getToken().getTokenNo().toString());
                }
                if(tokenDetails.getPrefix() != null){
                    tdm.setPrefix(tokenDetails.getPrefix().toString());
                }
                if(tokenDetails.getFirstName() != null){
                    tdm.setFirstName(tokenDetails.getFirstName());
                }
                if(tokenDetails.getMiddleInitial() != null){
                    tdm.setMiddleInitial(tokenDetails.getMiddleInitial());
                }
                if(tokenDetails.getLastName() != null){
                    tdm.setLastName(tokenDetails.getLastName());
                }
                if(tokenDetails.getMobileNo() != null){
                    tdm.setMobileNo(tokenDetails.getMobileNo());
                }
                if(tokenDetails.getEmailId() != null){
                    tdm.setEmailId(tokenDetails.getEmailId());
                }
                if(tokenDetails.getDestination() != null){
                    tdm.setDestinationId(tokenDetails.getDestination().getId());
                    tdm.setDestinationName(tokenDetails.getDestination().getName());
                }
                if(tokenDetails.getTokenStatus() != null){
                    tdm.setTokenStatus(tokenDetails.getTokenStatus().ordinal());
                }
                //System.out.println("TokenDetailsDTO: "+tdm);
                tokenDetailsList.add(tdm);
            }
        }
        return tokenDetailsList;
    }

    @Override
    public TokenStatusReplyModel tokenReply(Long companyId, Integer tokenNo) {
        List<Token> tokenList = tokenDAO.findTokensInQueue(companyId, tokenNo);
        System.out.println("TokenService >> toeknReply >> "+tokenList.size());
        TokenStatusReplyModel model = new TokenStatusReplyModel();
        if(tokenList.size() > 0){
            Token token = tokenList.get(tokenList.size()-1);
            TokenDetails details = token.getTokenDetails();
            model.setTokenNo(token.getTokenNo());
            model.setName(details.getFirstName()+" "+details.getMiddleInitial()+" "+details.getLastName());
            model.setCompanyId(companyId);
            model.setCompanyName(token.getCompany().getName());
            model.setDestinationName(details.getDestination().getName());
            model.setTokenPositionInQueue(tokenList.size());
            model.setStatus(token.getTokenDetails().getTokenStatus().name());
            model.setFrom("OPGEA");
        }
        return model;
    }

    

}
